<template>
    <div class="card-container">
        <div class="card-background" id="capture">
            <div class="card">
                <div class="common" id="common">
                    <div class="avatar1">
                        <img id="common-my-avatar" :src="myavatar" alt="">
                    </div>
                    <div class="avatar2">
                        <img id="common-ta-avatar" :src="hisavatar" alt="">
                    </div>
                </div>

                <div class="title">
                    <div class="nickname">
                        <span id="my-nickname">{{ mynickname }}</span>
                        <span>&</span>
                        <span id="ta-nickname">{{ hisnickname }}</span>
                    </div>
                    <div class="reportname">{{ reportName }}</div>
                </div>
                <div class="times">
                    <div class="sentence">
                        <div class="tag-template">第一次聊天在</div>
                        <div>
                            <span class="tag-content" id="first-time">{{ CardData.firstChatDate }}</span>
                        </div>
                    </div>

                    <div class="sentence">
                        <div class="tag-template">已经相识</div>
                        <div>
                            <span class="tag-content" id="time-from-today">{{ daysSinceFirstChat }}</span>
                            <span class="tag-template">天</span>
                        </div>
                    </div>

                    <div class="sentence">
                        <div>
                            <span class="tag-content" id="year">{{ CardData.mostChatYear }}</span>
                            <span class="tag-template">年</span>
                            <span class="tag-content" id="month">{{ CardData.mostChatMonth }}</span>
                            <span class="tag-template">月</span>
                        </div>
                        <div>
                            <span class="tag-template">发送了</span>
                            <span class="tag-content" id="month-num">{{ CardData.mostChatCount }}</span>
                            <span class="tag-template">条消息</span>
                        </div>
                        <div>
                            <span class="tag-template">对你的话说不完</span>
                        </div>
                    </div>

                </div>
                <div class="sum-up">
                    {{ summaryText }}
                </div>
                <div class="tags">
                    <div class="left">
                        <div v-for="tag in CardData.leftTags" :key="tag.label">
                            <div class="tag-template">{{ tag.label }}</div>
                            <div>
                                <span class="tag-content">{{ tag.value }}</span>
                                <span class="tag-template">{{ tag.unit }}</span>
                            </div>
                        </div>
                    </div>
                    <div class="right">
                        <div v-for="tag in CardData.rightTags" :key="tag.label">
                            <div class="tag-template">{{ tag.label }}</div>
                            <div v-if="tag.image">
                                <img :src="tag.image" alt="Tag Image">
                            </div>
                            <div v-else>
                                <span class="tag-content">{{ tag.value }}</span>
                            </div>
                        </div>
                        <div class="qrcode">
                            <img src="../../public/url_qrcode.svg" alt="QrCode">
                        </div>
                    </div>
                </div>
                <div class="copyright">
                    <img src="../../public/favicon.ico" alt="">
                    <span>memotrace.cn</span>
                </div>

                <div class="bottom">
                    <div>{{ bottomText }}</div>
                </div>
            </div>
        </div>
        <div class="qr-container" id="qr-container" v-show="qrshow">
            <div class="common" id="common">
                <div class="avatar1">
                    <img id="common-my-avatar" :src="myavatar" alt="">
                </div>
                <div class="avatar2">
                    <img id="common-ta-avatar" :src="hisavatar" alt="">
                </div>
                <div class="qr-title">
                    <div>2024</div>
                    <div>微信年度报告</div>
                </div>
            </div>

            <div class="url-qr" id="url-qr">
            </div>
            <div class="footer">
                <div>微信扫一扫查看年度报告</div>
            </div>
        </div>

    </div>
    <div class="buttons">
        <button @click="captureImage" class="welcome-button">
            下载卡片
            <div class="hoverEffect">
                <div></div>
            </div>
        </button>
        <button @click="uploadReport" class="upload-button">
            分享报告
            <div class="hoverEffect">
                <div></div>
            </div>
        </button>
        <button @click="captureQRCode" class="QRCode-button" v-show="qrshow">
            二维码下载
            <div class="hoverEffect">
                <div></div>
            </div>
        </button>
    </div>
</template>

<script setup>
import {
    key,
    wxid,
    reportName,
    bottomText,
    CardData,
    daysSinceFirstChat,
    cardName,
    firstChatData,
    chatData,
    chatdescription,
    Calendardata,
    keyWordsData,
    quarterData,
    myavatar,
    hisavatar,
    mynickname,
    hisnickname,
    summaryText,
    UPLOAD_URL
} from "../js/getdata.js";
import { ref } from "vue";
import QRcodestyling from 'qr-code-styling'
import domtoimage from 'dom-to-image';

const uploda_data = {
    myavatar: myavatar.value,
    hisavatar: hisavatar.value,
    mynickname: mynickname.value,
    hisnickname: hisnickname.value,
    firstChatData: firstChatData.value,
    totalChatData: chatData.value,
    timePeriodData: chatdescription.value,
    CalendarData: Calendardata.value,
    keyWordData: keyWordsData.value,
    Quarter_SummaryData: quarterData.value,
    summaryText: summaryText.value,
};
const qrshow = ref(false);


function captureImage() {
    cardName.value = `${CardData.value.mynickname}&${CardData.value.hisnickname}的2024年度报告`;
    var element = document.getElementById('capture');
    var scale = 2;
    var width = element.offsetWidth;
    var height = element.offsetHeight;
    domtoimage.toBlob(element, {
        width: width * scale,
        height: height * scale,
        style: {
            transform: 'scale(' + scale + ')',
            transformOrigin: 'top left',
            width: width + 'px',
            height: height + 'px'
        }
    }).then(function (blob) {
        var link = document.createElement('a');
        link.href = URL.createObjectURL(blob);
        link.download = cardName.value;
        document.body.appendChild(link);
        element.classList.remove('dark-style');
        link.click();
        document.body.removeChild(link);
    })
        .catch(function (error) {
            console.error('oops, something went wrong!', error);
        });
    console.log('captureImage');
}

function captureQRCode() {
    var element = document.getElementById('qr-container');
    var scale = 2;
    var width = element.offsetWidth;
    var height = element.offsetHeight;
    domtoimage.toBlob(element, {
        width: width * scale,
        height: height * scale,
        style: {
            transform: 'scale(' + scale + ')',
            transformOrigin: 'top left',
            width: width + 'px',
            height: height + 'px'
        }
    }).then(function (blob) {
        var link = document.createElement('a');
        link.href = URL.createObjectURL(blob);
        link.download = `${cardName.value}QRcode`;
        document.body.appendChild(link);
        element.classList.remove('dark-style');
        link.click();
        document.body.removeChild(link);
    })
        .catch(function (error) {
            console.error('oops, something went wrong!', error);
        });
    console.log('captureQrcode');
}

function uploadReport() {
    var userConfirmed = confirm("注意！！！\n本页面将会上传至云端，别人可通过链接查看该网页，该操作不会上传您的其他任何信息，页面将在下个版本更新之后删除，请注意链接生效时间。\n点击“确认”代表您同意上传，否则请点击“取消");
    if (userConfirmed) {
        // User clicked OK, perform the upload and display QR code
        uploadAndDisplayQRCode();
    } else {
        // User clicked Cancel or closed the dialog
        alert("Upload canceled.");
    }
}

function uploadAndDisplayQRCode() {
    // Simulate a GET request to the server endpoint
    displayQRCode("https://memotrace.cn/2024Report/");
}

function displayQRCode(url) {
    const qrCode = new QRcodestyling({
        width: 200,
        height: 200,
        type: "svg",
        data: url,
        cornersSquareOptions: {
            type: "extra-rounded", // 圆角方块，与主体一致
            gradient: {
                type: "linear",
                rotation: 90, // 同方向渐变
                colorStops: [
                    { offset: 0, color: "#d2cdff" }, // 起始色
                    { offset: 1, color: "#ffffd0" } // 结束色
                ]
            }
        },
        cornersDotOptions: {
            type: "dot", // 圆形，与主体圆角协调
            gradient: {
                type: "linear",
                rotation: 90, // 同方向渐变
                colorStops: [
                { offset: 0, color: "#d2cdff" }, // 起始色
                { offset: 1, color: "#ffffd0" } // 结束色
                ]
            }
        },
        dotsOptions: {
            type: "rounded", // 圆角
            gradient: {
                type: "linear",
                rotation: 90,
                colorStops: [
                { offset: 0, color: "#d2cdff" }, // 起始色
                { offset: 1, color: "#ffffd0" } // 结束色
                ]
            }
        },
        image: "",

        backgroundOptions: {
            color: "rgba(0,0,0,0)" // 设置背景颜色为透明
        },
    });
    qrCode.append(document.getElementById("url-qr"));
    qrshow.value = true;
}


</script>
<style scoped>
.card-container {
    display: flex;
    justify-content: center;
    align-items: center;
    height: 90vh;
    /* background-image: linear-gradient(180deg,#5ee7df,#b490ca); */
    perspective: 1000px;
}

.card-background {
    display: flex;
    justify-content: center;
    align-items: center;
    height: 800px;
    width: 450px;
    /* background-image: linear-gradient(200deg, #fbc2eb, #a6c1ee); */
    perspective: 1000px;
}

.card {
    position: relative;
    width: 360px;
    height: 580px;
    margin: 20px;
    /* background-image: linear-gradient(200deg, #a6c1ee, #fbc2eb, #fff); */
    background: radial-gradient(200% 100% at bottom center, #f7f7b6, #e96f92, #75517d);
    background: radial-gradient(220% 105% at top center, #75517d 40%, #e96f92 65%, #f7f7b6);
    border-radius: 20px;
    overflow: hidden;
    display: flex;
    flex-direction: column;
    align-items: center;
    color: #fff;
    box-shadow: 0 0 30px rgba(0, 0, 0, 0.5);
    flex-shrink: 0;
    animation: fadeIn 1s;
}

@keyframes fadeIn {
    0% {
        opacity: 0;
        transform: scale(0.8);
    }

    100% {
        opacity: 1;
        transform: scale(1);
    }
}

.card-background {
    display: flex;
    justify-content: center;
    align-items: center;
    height: 800px;
    width: 450px;
    perspective: 1000px;
}

.card {
    position: relative;
    width: 360px;
    height: 580px;
    margin: 20px;
    border-radius: 20px;
    overflow: hidden;
    display: flex;
    flex-direction: column;
    align-items: center;
    color: #fff;
    box-shadow: 0 0 30px rgba(0, 0, 0, 0.5);
    flex-shrink: 0;
}

.card .common .avatar1,
.card .common .avatar2 {
    position: absolute;
    top: 30px;
    width: 60px;
    height: 60px;
    border-radius: 50%;
    box-shadow: 0 0 20px rgba(85, 59, 59, 0.8);
    overflow: hidden;
}

.card .love .avatar1,
.card .love .avatar2 {
    position: absolute;
    top: 35px;
    width: 65px;
    height: 65px;
    border-radius: 50%;
    box-shadow: 0 0 10px rgb(255, 112, 112, 0.5);
    overflow: hidden;
}

.avatar1 img,
.avatar2 img {
    width: 100%;
    height: 100%;
    object-fit: cover;
}

.avatar1::before,
.avatar1::before {
    content: "";
    position: absolute;
    width: 100%;
    height: 100%;
    /* background: linear-gradient(to bottom, transparent 50%, #af7777); */
}

.card .common .avatar1 {
    left: 30px;
}

.card .common .avatar2 {
    left: 80px;
}

.card .avatar {
    position: absolute;
    top: 30px;
    left: 30px;
    width: 80px;
    height: 80px;
    border-radius: 50%;
    box-shadow: 0 0 20px rgba(180, 168, 168, 0.8);
    overflow: hidden;
}

.card .avatar img {
    width: 100%;
    height: 100%;
    object-fit: cover;
}

.card .avatar::before {
    content: "";
    position: absolute;
    width: 100%;
    height: 100%;
    background: linear-gradient(to bottom, transparent 50%, #222);
}

.card .title {
    position: absolute;
    max-width: 160px;
    top: 20px;
    right: 30px;
    color: #333;
}

.card .title .nickname {
    font-size: 14px;
}

.card .title .reportname {
    font-size: 18px;
    font-weight: bold;
}

.card .times {
    position: absolute;
    top: 124px;
    left: 34px;
    display: block;
    color: #333;

}

.card .times .sentence {
    margin: 8px 0;
}

.card .sum-up {
    position: absolute;
    top: 80px;
    right: 30px;
    height: 245px;
    color: #333;
    font-size: 48px;
    font-family: 黑体;
    font-weight: bold;
    writing-mode: vertical-rl;
}

.card .tags {
    position: absolute;
    width: 100%;
    height: 200px;
    top: 340px;
    display: flex;
}

.card .tags .left,
.card .tags .right {
    width: 50%;
    color: #333;
}

.card .tags .left {
    padding-left: 34px;
}

.card .tags .right img {
    max-width: 50px;
}

.card .tags .right .qrcode {
    position: absolute;
    bottom: 0;
    right: 30px;
    bottom: -5px;
    z-index: 1;
}

.card .tags .right .qrcode img {
    width: 50px;
    height: 50px;
}

.tag-template {
    font-size: 14px;
    /* font-weight: bold; */
    margin: 5px 0px 2px 0px;
    font-family: 黑体;
}

.tag-content {
    font-size: 20px;
    font-weight: bold;

}

.card .copyright {
    position: absolute;
    height: 24px;
    bottom: 10px;
    left: 18px;
}

.card .copyright img {
    width: 20px;
    height: 20px;
    border-radius: 50%;
    overflow: hidden;
    vertical-align: middle
}

.card .copyright span {
    font-size: 12px;
    font-family: Arial, Helvetica, sans-serif;
    font-weight: bold;
    color: #333;
    vertical-align: middle
}

.card .url {
    position: absolute;
    top: 20px;
    right: 30px;
    font-size: 14px;
}

.card .url a {
    text-decoration: none;
    color: #5c5c5c;
    font-size: 10px;
}

.card .bottom {
    position: absolute;
    bottom: 12px;
    left: 165px;
    color: #8f8383;
    font-size: 11px;
    text-align: right;
}

.card .bottom a {
    text-decoration: none;
    color: #8f8383;
}

.upload-button,
.welcome-button,
.QRCode-button {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    padding: 15px 30px;
    border: 0;
    position: absolute;
    overflow: hidden;
    border-radius: 10rem;
    font-weight: bold;
    cursor: pointer;
    color: rgb(37, 37, 37);
    z-index: 1;
    box-shadow: 0 0px 7px -5px rgba(0, 0, 0, 0.5);
    bottom: 5%;
    left: 50%;
}

.upload-button {
    transform: translateX(calc(-100% + 160px));
}

.welcome-button {
    transform: translateX(-160px);
}

.QRCode-button {
    transform: translateX(calc(-100% + 310px));
}

.welcome-button:hover,
.upload-button:hover,
.QRCode-button:hover {
    background: rgb(193, 228, 248);
    color: rgb(33, 0, 85);
}

.hoverEffect {
    position: absolute;
    bottom: 0;
    top: 0;
    left: 0;
    right: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 1;
}

.hoverEffect div {
    background: rgb(222, 0, 75);
    background: linear-gradient(90deg,
            rgba(222, 0, 75, 1) 0%,
            rgba(191, 70, 255, 1) 49%,
            rgba(0, 212, 255, 1) 100%);
    border-radius: 40rem;
    width: 10rem;
    height: 10rem;
    transition: 0.4s;
    filter: blur(20px);
    animation: effect infinite 3s linear;
    opacity: 0.5;
}

.welcome-button:hover .hoverEffect div {
    width: 8rem;
    height: 8rem;
}

.upload-button:hover .hoverEffect div {
    width: 8rem;
    height: 8rem;
}

.QRCode-button:hover .hoverEffect div {
    width: 8rem;
    height: 8rem;
}

.qr-container {
    position: relative;
    width: 300px;
    height: 400px;
    box-shadow: 0 0 30px rgba(0, 0, 0, 0.5);
    border-radius: 18px;
    background: radial-gradient(200% 100% at bottom center, #f7f7b6, #e96f92, #75517d);
    background: radial-gradient(220% 105% at top center, #75517d 40%, #e96f92 65%, #f7f7b6);
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: space-between;
    /* align-content: center; */
}

.qr-container .common{
    margin-top: 50px;
    display: flex;
    align-items: center;
}

.qr-container .common .avatar1,
.qr-container .common .avatar2 {
    /* position: absolute;
    top: 34px; */
    width: 50px;
    height: 50px;
    border-radius: 50%;
    box-shadow: 0 0 20px rgba(85, 59, 59, 0.8);
    overflow: hidden;
}

.qr-container .common .qr-title {
    position: absolute;
    left: 150px;
    font-weight: 700;
    margin-left: 10px;
}

.qr-container .common .avatar1 {
    position: absolute;
    left: 50px;
}

.qr-container .common .avatar2 {
    position: absolute;
    left: 90px;
}

.qr-container .url-qr {
    /* position: absolute;
    top: 80px; */
    margin-top: 30px;
    /* margin-left: 50px; */
}
.footer{
    text-align: center;
    color: #8f8383;
    font-size: 11px;
    margin-bottom: 20px;
    /* position: absolute;
    bottom: 10px;
    left: 100px; */
}

@keyframes effect {
    0% {
        transform: rotate(0deg);
    }

    100% {
        transform: rotate(360deg);
    }
}

@media screen and (max-width: 1080px) {
    .buttons {
        display: none;
    }
}

@media screen and (max-width: 400px) {
    .card {
        transform: scale(0.8);
    }

    .card .sum-up {
        font-size: 42px;
    }

    @keyframes fadeIn {
        0% {
            opacity: 0;
            transform: scale(0.5);
        }

        100% {
            opacity: 1;
            transform: scale(0.8);
        }
    }
}
</style>